#Twelve Sky parser

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class TwelveSky_MDL(Model3D):
    '''Replace 'TwelveSky_MDL' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(TwelveSky_MDL,self).__init__("MDL")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_animation(self):
        
        pass
        
    def parse_material(self, filename):
        
        self.create_material(0)
        self.add_material_name(0, filename)
        self.add_texture_name(0, filename + ".png")
        
    def parse_vertices(self, numVerts, meshName):
        
        for i in range(numVerts):
                
            vx, vy, vz = self.inFile.read_float(3)
            nx, ny, nz = self.inFile.read_float(3)
            tu, tv = self.inFile.read_float(2)
            
            vx = float(vx) * -1
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
            
    def parse_faces(self, numFaces, meshName):
        
        for i in range(numFaces):
            v1, v2, v3 =  self.inFile.read_short(3)
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, 0)
    
    def parse_file(self, filename):
        '''Main parser method. Can be replaced'''
        
        meshName = filename
        self.create_object(meshName)
        self.parse_material(filename)
        
        unk1, meshType = self.inFile.read_long(2)
        emission = self.inFile.read_float()
        color1 = self.inFile.read_float(4)
        color2 = self.inFile.read_float(4)
        
        self.inFile.seek(76, 1)
        numMatrix = self.inFile.read_long()
        numVerts = self.inFile.read_long()
        unk = self.inFile.read_long()
        numFaces = self.inFile.read_long()
        
        if meshType == 0:
        
            self.inFile.seek(40, 1)
            self.parse_vertices(numVerts, meshName)
                
            for i in range(numMatrix):
                self.inFile.read_float(8)
            
            self.parse_faces(numFaces, meshName)
        elif meshType == 1:
            
            print numVerts
            for i in range(numMatrix):
                self.inFile.read_float(16)
            self.parse_vertices(numVerts, meshName)
            print self.inFile.tell()
            self.parse_faces(numFaces, meshName)
        
        else:
            print "unknown mesh type", meshType
        
def read_file(path):
    '''Read the file'''
    
    filename, ext = os.path.splitext(os.path.basename(path))
    print filename
    openFile = StructReader(open(path, 'rb'))
    obj = TwelveSky_MDL(inFile=openFile)
    obj.parse_file(filename)
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "TSMDL", "Twelve Sky TSMDL"

if __name__ == '__main__':
    
    file1 = "C001001001_01.mdl"
    file2 = "C001001005_01.mdl"
    obj = read_file(file1)
    